package com.ywwxhz.app;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.MenuItem;

import com.ywwxhz.Util.Config;
import com.ywwxhz.Util.UIKit;
import com.ywwxhz.app.fragment.GeneralPreferenceFragment;
import com.ywwxhz.app.fragment.IFragmentShouldExit;
import com.ywwxhz.lib.SystemBarTintManager;
import com.ywwxhz.taskmgr.R;

import java.lang.reflect.Field;

public class SettingsActivity extends PreferenceActivity implements UIKit.Translucentable {
    private SystemBarTintManager mSystemBarTintManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        UIKit.setBackIcon(getActionBar(), getResources().getDrawable(R.drawable.ic_menu_back));
        PreferenceManager.setDefaultValues(getApplicationContext(), Config.CONFIG_NAME, Context.MODE_MULTI_PROCESS, R.xml.pref_general, false);
        PreferenceManager.setDefaultValues(getApplicationContext(), Config.CONFIG_NAME, Context.MODE_MULTI_PROCESS, R.xml.pref_plugins, false);
        PreferenceManager.setDefaultValues(getApplicationContext(), Config.CONFIG_NAME, Context.MODE_MULTI_PROCESS, R.xml.pref_laboratory, false);
        getFragmentManager().beginTransaction().replace(android.R.id.content,
                new GeneralPreferenceFragment()).commit();
        mSystemBarTintManager = UIKit.buildTranslucentStatus(this, false, getResources().getColor(R.color.task_statusbar_bg));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    private Fragment getFragmentFromBackStack(FragmentManager.BackStackEntry backStackEntry) {
        try {
            Field mTail = backStackEntry.getClass().getDeclaredField("mTail");
            mTail.setAccessible(true);
            Object mTailo = mTail.get(backStackEntry);
            Field fragment = mTailo.getClass().getDeclaredField("fragment");
            fragment.setAccessible(true);
            return (Fragment) fragment.get(mTailo);
        } catch (Exception ignored) {
        }
        return null;

    }

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() == 0) {
            setResult(RESULT_OK, getIntent());
            this.finish();
            System.gc();
        } else {
            FragmentManager.BackStackEntry backStackEntry = getFragmentManager().getBackStackEntryAt(0);
            Fragment fragment = getFragmentFromBackStack(backStackEntry);
            if (((IFragmentShouldExit) fragment).getShouldExit()) {
                getActionBar().setSubtitle(R.string.pref_basic);
                getFragmentManager().popBackStack();
                System.gc();
            }
        }
    }

    public SystemBarTintManager getSystemBarTintManager() {
        return mSystemBarTintManager;
    }
}
